package com.health.healthproject.config;

import cn.dev33.satoken.config.SaTokenConfig;
import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.stp.StpUtil;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class SaTokenConfiguration implements WebMvcConfigurer {

    /**
     * 跨域配置
     */
   /* @Bean
    public CorsFilter corsFilter() {
        CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true);
        // 允许所有域名跨域
        config.addAllowedOriginPattern("*");
        // 允许所有请求头
        config.addAllowedHeader("*");
        // 允许所有方法
        config.addAllowedMethod("*");

        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", config);
        return new CorsFilter(source);
    }*/

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册Sa-Token拦截器，鉴权
        registry.addInterceptor(new SaInterceptor(handle -> StpUtil.checkLogin()))
                .addPathPatterns("/**")  // 拦截所有请求
                .excludePathPatterns(    // 排除不需要鉴权的接口
                        "/user/login",   // 登录接口
                        "/user/insert", // 注册接口
                        "/static/**",    // 静态资源
                        "/favicon.ico",  // 网站图标
                        "/error/**",     // 错误页面
                        "/api/user/insert",  // 添加用户
                        "/*.html",       // HTML静态资源
                        "/*.css",        // CSS静态资源
                        "/*.js",         // JS静态资源
                        "/webjars/**",   // WebJars资源
                        "/swagger/**",   // Swagger文档
                        "/v3/api-docs/**", // OpenAPI文档
                        "/swagger-ui/**"  // Swagger UI
                );
    }
}